package com.ql.mangersystem.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.po.Orders;
import com.ql.mangersystem.service.OrdersService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping("/web")
@RestController
@Slf4j
@Api("echarts接口")
@CrossOrigin(value = "http://localhost:5173/")
public class WebController {
    @Resource
    private OrdersService ordersService;
    @GetMapping("/line")
    public Result line(@RequestParam("userid")Integer userid) {
        // 包装折线图的数据
        List<Orders> list = ordersService.list(userid);
        Set<String> dates = list.stream().map(Orders::getDate).collect(Collectors.toSet());
        List<String> dateList = CollUtil.newArrayList(dates);
        dateList.sort(Comparator.naturalOrder());
        List<Dict> lineList = new ArrayList<>();
        for (String date : dateList) {
            // 统计当前日期的所有金额总数和
            BigDecimal sum = list.stream().filter(orders -> orders.getDate().equals(date)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict line = dict.set("date", date).set("value", sum);
            lineList.add(line);
        }
        Dict res = Dict.create().set("line", lineList);
        return Result.success(res);
    }
    @GetMapping("/bar")
    public Result bar(@RequestParam("userid")Integer userid) {
        List<Orders> list = ordersService.list(userid);
        // 柱状图数据
        List<Dict> barList = new ArrayList<>();
        Set<String> categories = list.stream().map(Orders::getCategory).collect(Collectors.toSet());
        for (String cate : categories) {
            // 统计当前日期的所有金额总数和
            BigDecimal sum = list.stream().filter(orders -> orders.getCategory().equals(cate)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict bar = dict.set("name", cate).set("value", sum);
            barList.add(bar);
        }
        Dict res = Dict.create().set("bar", barList);
        return Result.success(res);
    }
    @GetMapping("/pie")
    public Result pie(@RequestParam("userid")Integer userid) {
        List<Orders> list = ordersService.list(userid);
        // 饼图数据
        List<Dict> pieList = new ArrayList<>();
        Set<String> categories = list.stream().map(Orders::getCategory).collect(Collectors.toSet());
        for (String cate : categories) {
            // 统计当前日期的所有金额总数和
            BigDecimal sum = list.stream().filter(orders -> orders.getCategory().equals(cate)).map(Orders::getMoney)
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            Dict dict = Dict.create();
            Dict pie = dict.set("name", cate).set("value", sum);
            pieList.add(pie);
        }
        Dict res = Dict.create().set("pie   ", pieList);
        return Result.success(res);

    }
}
